/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipinyou.wordstatistics.handler;

import com.ipinyou.wordstatistics.core.HandleResult;
import com.ipinyou.wordstatistics.core.ISearchResultHandler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 *
 * @author xiangnan.wang@ipinyou.com
 */
public class ChineseWordHandler implements ISearchResultHandler{

    private boolean checkFlag=true;
    
    private Pattern p=Pattern.compile("\"([^\"]*[\\u4e00-\\u9fbb]+[^\"]*)\"");
    
    public HandleResult handler(File f) {
        HandleResult result=null;
        try {
            BufferedReader reader=new BufferedReader(new FileReader(f));
            String data=null;
            result=new HandleResult();
            result.setFileName(f.getAbsolutePath());
            int ln=1; //行号
            while((data=reader.readLine())!=null){
                if(checkChineseWord(data)){
//                    HandleResult.CodeInfo codeInfo=new HandleResult.CodeInfo(data,ln);
                    List<HandleResult.CodeInfo> cil=parseCode(data,ln);
                    result.addCodeInfos(cil);
                }
                ln++;
            }
            reader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ChineseWordHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ChineseWordHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    /**
     * 判断是否包含中文
     * @return true  false
     */
    private boolean checkChineseWord(String data){
        data=data.trim();
        if(checkFlag){
            //没有注释
            if(data.matches("^\\/\\*\\*")){
                checkFlag=false;
            }else{
                if(data.contains("//")){
                    int index=data.indexOf("//");
                    if(index>0){
                        data=data.substring(0,index);
                        if(data.matches(".*[\\u4e00-\\u9fbb]+.*")){
                            return true;
                        }
                    }
   
                }else{
                    if(data.matches(".*[\\u4e00-\\u9fbb]+.*")){
                        return true;
                    }
                }
            }
        }else{
            if(data.contains("*/")){
                checkFlag=true;
            }
        }
        return false;
    }
    
    public List<HandleResult.CodeInfo> parseCode(String code,int lineNum){
        List<HandleResult.CodeInfo> rst=new ArrayList<HandleResult.CodeInfo>();
        boolean mark=false;   //是否包含"号
        boolean flag=false;     //是否包含汉字
        boolean lastFlag=false;
        int start=-1;           //字符开始的位置
        int end=-1;             //字符结束位置
        for(int i=0;i<code.length();i++){
            int c=code.charAt(i);
            if(c==34){
                mark=!mark;
                if(!mark&&start>0){
                    lastFlag=false;
                    end=i;
                }
            }
            else if(mark&&flag){
                lastFlag=true;
            }
            else if(c>=0x4e00&&c<=0x9fbb){
                flag=true;
                lastFlag=true;
                if(start<0){
                    start=i;
                }
            }else{
                if(start>=0){
                    end=i;
                }
                lastFlag=false;
            }
            if((!lastFlag||i==(code.length()-1))&&flag){
                flag=false;
                if(end<0){
                    end=code.length();
                }
                HandleResult.CodeInfo ci=new HandleResult.CodeInfo(code.substring(start, end),lineNum,start,end);
                rst.add(ci);
                start=-1;
                end=-1;
            }
        }
        
        return rst;
    }   


//    public static void main(String args[]){
//        ChineseWordHandler tmp=new ChineseWordHandler();
//        System.out.println(tmp.checkChineseWord("System.out.println(\"斯蒂芬范德萨\") // 算法第三方"));
//       System.out.println("(斯蒂芬范德萨 ".matches(".*[\\u4e00-\\u9fbb]+.*"));
//    }

}